<%#
 Copyright 2013-2018 the original author or authors from the JHipster Phipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%=packageName%>.web.rest.<%=context.extraInstance%>;

import com.codahale.metrics.annotation.Timed;
import <%=packageName%>.domain.<%=context.entityClass%>;
import <%=packageName%>.service.<%=context.extraInstance%>.<%=context.entityClass%><%=context.extraName%>Service;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

@Api(value = "<%=options.entity.definition.javadoc%>", tags = {"<%=options.entity.definition.javadoc%>说明"})
@RestController
@RequestMapping("/api/<%=context.extraInstance%>")
public class <%=context.entityClass%><%=context.extraName%>Resource {

    private final Logger log = LoggerFactory.getLogger(<%=context.entityClass%><%=context.extraName%>Resource.class);

    private final <%=context.entityClass%><%=context.extraName%>Service <%=context.entityInstance%><%=context.extraName%>Service;

    private static final String ENTITY_NAME = "<%=context.entityInstance%>";

    public <%=context.entityClass%><%=context.extraName%>Resource(<%=context.entityClass%><%=context.extraName%>Service <%=context.entityInstance%><%=context.extraName%>Service) {
    this.<%=context.entityInstance%><%=context.extraName%>Service = <%=context.entityInstance%><%=context.extraName%>Service;
    }

    /**
     * PATCH  /<%=context.entityApiUrl%> : 更新一个已存在的 <%=context.entityInstance%>.
     *
     * @param <%=context.entityInstance%> 需要更新的 <%=context.entityInstance%> 实体
     * @return 返回 200 OK 并更新用户传入的 <%=context.entityInstance%> 中的字段,
     * 如果用户传入的 <%=context.entityInstance%> 验证失败则返回 400 (Bad Request),
     * 如果 <%=context.entityInstance%> 不能被更新则返回 500 (服务器内部错误)
     * @throws URISyntaxException 如果 URI 异常则抛出此错误
     */
    @ApiOperation(value = "更新<%=options.entity.definition.javadoc%>", notes = "更新一个已存在的 <%=context.entityInstance%>, 传入需要更新的字段即可")
    @ApiResponses({
        @ApiResponse(code = 403, message = "禁止访问"),
        @ApiResponse(code = 401, message = "未授权"),
        @ApiResponse(code = 404, message = "实体不存在"),
        @ApiResponse(code = 204, message = "更新成功")
    })
    @PatchMapping("/<%=context.entityApiUrl%>")
    public ResponseEntity<<%=context.entityClass%>> patch<%=context.entityClass%>(@ApiParam(value = "<%=context.entityInstance%>参数", required = true)
        @RequestBody <%=context.entityClass%> <%=context.entityInstance%>) throws URISyntaxException {
        log.debug("REST request to save <%=context.entityClass%> : {}", <%=context.entityInstance%>);
        Optional<<%=context.entityClass%>> result = <%=context.entityInstance%><%=context.extraName%>Service.save(<%=context.entityInstance%>);
        return ResponseUtil.wrapOrNotFound(result);
    }

    /**
     * POST  /<%=context.entityApiUrl%> : 创建一个新的 <%=context.entityInstance%>.
     *
     * @param <%=context.entityInstance%>S 需要被创建的 <%=context.entityInstance%> 实体
     * @return 返回创建成功 201 (已创建)，并且返回的body域中包含新的<%=context.entityInstance%>, 如果用户传入的 <%=context.entityInstance%> 中包含 id，则返回 400 (Bad Request)。
     * @throws URISyntaxException 如果 URI 异常则抛出此错误
     */
    @ApiOperation(value = "新增<%=context.entityInstance%>", notes = "创建列表中的<%=context.entityInstance%>，如果编号已经存在则更新")
        @ApiResponses({
        @ApiResponse(code = 401, message = "未授权"),
        @ApiResponse(code = 201, message = "创建成功"),
        @ApiResponse(code = 403, message = "禁止访问"),
        @ApiResponse(code = 404, message = "页面未找到")
    })
    @PostMapping("/<%=context.entityApiUrl%>")
    @Timed
    public ResponseEntity<List<<%=context.entityClass%>>> create<%=context.entityClass%>(@ApiParam(value = "<%=context.entityInstance%>列表", required = true)
            @RequestBody List<<%=context.entityClass%>> <%=context.entityInstance%>S) throws URISyntaxException {
        log.debug("REST request to save <%=context.entityClass%>s : {}", <%=context.entityInstance%>S);
        List<<%=context.entityClass%>> result = <%=context.entityInstance%><%=context.extraName%>Service.save(<%=context.entityInstance%>S);
        return ResponseEntity.created(new URI("/api/<%=context.entityInstance%>/<%=context.entityApiUrl%>"))
            .body(result);
    }

    /**
     * PUT  /<%=context.entityApiUrl%> : 更新一个已经存在的 <%=context.entityInstance%>.
     *
     * @param <%=context.entityInstance%>S 需要被更新的 <%=context.entityInstance%> 实体，这里所有字段都将被更新
     * @return 返回 200 (OK) 并返回已经更新完毕的 <%=context.entityInstance%> 实体,
     * 如果用户传入的 <%=context.entityInstance%> 验证失败则返回 400 (Bad Request),
     * 如果 <%=context.entityInstance%> 不能被更新则返回 500 (服务器内部错误)
     * @throws URISyntaxException 如果 URI 异常则抛出此错误
     */
    @ApiOperation(value = "创建或更新<%=context.entityInstance%>", notes = "创建或更新列表中的<%=context.entityInstance%>")
    @ApiResponses({
        @ApiResponse(code = 401, message = "未授权"),
        @ApiResponse(code = 200, message = "创建/更新成功"),
        @ApiResponse(code = 201, message = "创建/更新成功"),
        @ApiResponse(code = 403, message = "禁止访问"),
        @ApiResponse(code = 404, message = "页面未找到")
    })
    @PutMapping("/<%=context.entityApiUrl%>")
    @Timed
    public ResponseEntity<List<<%=context.entityClass%>>> update<%=context.entityClass%>(@ApiParam(value = "<%=context.entityInstance%>列表", required = true)
                        @RequestBody List<<%=context.entityClass%>> <%=context.entityInstance%>S) throws URISyntaxException {
        log.debug("REST request to update <%=context.entityClass%>s : {}", <%=context.entityInstance%>S);
        List<<%=context.entityClass%>> result = <%=context.entityInstance%><%=context.extraName%>Service.save(<%=context.entityInstance%>S);
        return ResponseEntity.ok().body(result);
    }

    /**
     * DELETE  /<%=context.entityApiUrl%>: 删除列表中所有的 <%=context.entityInstance%> 实体.
     *
     * @param <%=context.entityInstance%>S 包含 id 的<%=context.entityInstance%>S 列表
     * @return 返回状态码 200 (OK)
     */
    @ApiOperation(value = "删除<%=options.entity.definition.javadoc%>", notes = "删除 <%=context.entityInstance%>")
    @ApiResponses({
        @ApiResponse(code = 403, message = "禁止访问"),
        @ApiResponse(code = 401, message = "未授权"),
        @ApiResponse(code = 200, message = "删除成功"),
        @ApiResponse(code = 404, message = "实体未找到")
    })
    @DeleteMapping("/<%=context.entityApiUrl%>")
    @Timed
    public ResponseEntity<Void> delete<%=context.entityClass%>(@RequestBody List<<%=context.entityClass%>> <%=context.entityInstance%>S) {
        log.debug("REST request to delete <%=context.entityClass%> : {}", <%=context.entityInstance%>S);
        <%=context.entityInstance%><%=context.extraName%>Service.delete(<%=context.entityInstance%>S);
        return ResponseEntity.ok().build();
    }
}
